#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/5/16 12:04
# @Author  : liutianwei
# @File    : FAFRFixture.py
# @Software: PyCharm

import os
import pdb
import allure
import pytest
from _pytest.fixtures import SubRequest
from src.utils.PasrCalcRecrate import PasrCalcRecrate


@pytest.fixture(scope="class", autouse=False, name="ASRFARateCalculation")
def ASR_FA_Rate_Calculation(request: SubRequest):
    # 创建FAFR文件夹
    fafr_dir = os.path.join(request.cls.conveyor.suite_dir, 'FA_FR_dir')
    os.mkdir(fafr_dir)
    yield
    base_class = request.cls
    pasr_calc = PasrCalcRecrate(fafr_dir)
    pasr_calc.write_ref_and_asr(base_class.calc_recrate)
    suminfo = pasr_calc.start_fun()
    result_list = []
    try:
        expect_asr_result = float(request.cls.conveyor.case_header.faPassRate.value) * 100
        result_list.append(expect_asr_result)
        if expect_asr_result > 100 or expect_asr_result < 1:
            print("@FA_PASS_RATE value should be between 0.0 to 1.0")
    except AttributeError:
        print("@FA_PASS_RATE value Formatting error,such as 0.95")
        return False
    for sum_in in suminfo.split(' '):
        if sum_in.endswith('(Total)'):
            sum_in = sum_in.split("(")[0].split('\n')[1]
            result_list.append(sum_in)
        elif sum_in.endswith('(Right)'):
            right_case_num = sum_in.split("-")[0]
            right_case_rate = sum_in.split("%")[0].split('-')[-1]
            result_list.append(right_case_num)
            result_list.append(right_case_rate)
        elif sum_in.endswith('(Wrong)'):
            wrong_case_num = sum_in.split("-")[0]
            wrong_case_rate = sum_in.split("%")[0].split('-')[-1]
            result_list.append(wrong_case_num)
            result_list.append(wrong_case_rate)
    end_to_write_result(base_class.conveyor.suite_dir, result_list)
    print("=" * 10 + f" CalcRecrate file in {base_class.conveyor.suite_dir}/result.txt " + "=" * 10, )
    fa_fr_txt_path = os.path.join(fafr_dir, 'rate.txt')
    allure.attach.file(source= fa_fr_txt_path, name= 'fa_fr', attachment_type=allure.attachment_type.TEXT)


def end_to_write_result(default_path, fa_result):
    with open(os.path.join(default_path, "result.txt"), "a", encoding="utf-8") as f:
        f.write(f"The expected accuracy rate is : {fa_result[0]} \n")
        f.write(f"The actual correct rate is : {fa_result[3]} \n")
        if float(fa_result[0]) >= float(fa_result[3]):
            f.write(f"FA calculate accuracy not PASS\n")
        else:
            f.write(f"FA calculate accuracy PASS\n")
        f.write(f"TOTOL {fa_result[1]} case, RIGHT case: {fa_result[2]}, WRONG case: {fa_result[4]} \n")


@pytest.fixture(autouse=False, scope='function', name='ASRPasrResultFixture')
def ASR_Pasr_Result_Fixture(request: SubRequest):
    yield
    base_class = request.cls
    calc = {"voice": base_class.testcase.input.get('voice'), 'expect': base_class.asserter.expect_data,
            'actual': base_class.asserter.actual_data}
    base_class.calc_recrate.append(calc)
